<!doctype html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport"
        content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>js对象之封装抽象</title>
</head>
<body>
  <ul>
    <li>可通过闭包和作用域概念，定制内部私有的属性和方法，仅暴露必要的成员给外部</li>
  </ul>
  <script>
    // 对象封装
    function User(name, age) {
      // 私有属性
      const item = { name, age }
      // 私有方法
      const detail = function () {
        const status = item.age < 60 ? 'working' : 'retired'
        console.log(`${item.name} is ${status}.`)
      }
      // 暴露方法
      this.show = function () {
        console.log(`${item.name} is ${item.age} years old.`)
        detail()
      }
    }
    // Miracle is 35 years old.
    // Miracle is working.
    const user = new User('Miracle', 35)
    user.show()
  </script>
</body>
</html>